package sv.mecca.modelo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import sv.mecca.form.Clinica3Form;

public class Clinica3Modelo extends Conexion {
	private static final long serialVersionUID = 8990425447064615536L;

	public void saveClinica3(Clinica3Form c3) throws SQLException {
		try {
			save("INSERT INTO clinicas(id_tipo_clinica, id_paciente, id_usuario, tratamiento, diagnostico) VALUES("
					+ c3.getId_tipo_clinica()
					+ ", "
					+ c3.getId_paciente()
					+ ", "
					+ c3.getId_usuario()
					+ ", '"
					+ c3.getTratamiento()
					+ "', '" 
					+ c3.getDiagnostico() + "')");
		} catch (Exception e) {
			System.out
					.println("Error al guardar la clinica3 " + e.getMessage());
		}
	}

	/**
	 * Metodo para actualizar la clinica
	 * 
	 * 
	 * @throws SQLException
	 */
	public void updateClinica3(Clinica3Form c3) throws SQLException {
		try {
			update("UPDATE clinicas SET id_tipo_clinica = "
					+ c3.getId_tipo_clinica() + ", id_paciente = "
					+ c3.getId_paciente() + ", id_usuario = "
					+ c3.getId_usuario() + ", tratamiento = '"
					+ c3.getTratamiento() + "', diagnostico = '"
					+ c3.getDiagnostico() + "' WHERE id_clinica = "
					+ c3.getId_clinica());
		} catch (Exception e) {
			System.out.println("Error al actualizar la clinica3 "
					+ e.getMessage());
		}
	}

	/**
	 * Metodo para borrar la clinica
	 * 
	 * 
	 * @throws SQLException
	 */
	public void deleteClinica3(Clinica3Form u) throws SQLException {
		try {
			delete("DELETE FROM clinicas WHERE id_clinica = "
					+ u.getId_clinica());
		} catch (Exception e) {
			System.out.println("Error al eliminar la clinica3 "
					+ e.getMessage());
		}
	}

	/**
	 * Metodo para listar a todas las clinicas
	 * 
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Clinica3Form> listado() throws SQLException {
		ArrayList<Clinica3Form> c3 = new ArrayList<Clinica3Form>();
		ResultSet rs = executeQueryList("SELECT * FROM clinicas"
				+ " c INNER JOIN tipos_clinicas tu ON c.id_tipo_clinica = tu.id_tipo_clinica"
				+ " inner join pacientes p on c.id_paciente = p.id_paciente"
				+ " inner join usuarios u on c.id_usuario = u.id_usuario");
		while (rs.next()) {
			Clinica3Form t = new Clinica3Form(rs.getInt(1), rs.getInt(2),
					rs.getString("tipo_clinica"), rs.getInt(3), rs.getInt(4),
					rs.getString("id_usuario"), rs.getString(5),
					rs.getString(6));
			c3.add(t);
		}
		return c3;
	}
}
